package io.wmy.supex.modules.demo.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import io.wmy.supex.framework.annotation.PrintLog;
import io.wmy.supex.common.bean.JdbcProperty;
import io.wmy.supex.common.bean.MyContent;
import io.wmy.supex.modules.demo.model.User;
import io.wmy.supex.modules.demo.service.IUserService;

import javax.annotation.Resource;


/**
 * 示例功能控制器
 * 用于演示本项目单一功能
 */
@PrintLog(pretty = true)
@Controller
@RequestMapping("/demo/test")
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Resource
    private IUserService userService;

    @Autowired
    private JdbcProperty jdbcProperty;

    @Autowired
    private MyContent myContent;


    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable Integer id) {
        logger.info("jdbcProperty，url：{}；urlExt：{}；username：{}；password：{}",
                new Object[]{jdbcProperty.getUrl(),jdbcProperty.getUrlExt(), jdbcProperty.getUsername(), jdbcProperty.getPassword()});
        User user = userService.getUser(id);
        return user;
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "/index";
    }

    @RequestMapping(value = "/ftl", method = RequestMethod.GET)
    public ModelAndView ftl() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("test");
        mav.addObject("message", "你好freemarker show!!");
        return mav;
    }

    /**
     * 演示不同后缀请求，响应不同文件格式
     * 可测试后缀有：
     * /content->jsp
     * /content.json->json
     * /content.xls->xls
     * /content.xml->xml
     * /content.pdf->pdf
     * @return
     */
    @RequestMapping(value="/content", method=RequestMethod.GET)
    public ModelAndView getContent() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/admin/ext/result");
        mav.addObject("contents", myContent.createContent());
        return mav;
    }
}
